//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.gui.fixed;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.text.Format;
import javax.swing.*;

public class LabelAndFormattedTextField extends JPanel {

    public LabelAndFormattedTextField() {
        jFormattedTextField = null;
        jLabel = null;
        val = "N/A";
        initialize(0);
    }

    public LabelAndFormattedTextField(Format format) {
        jFormattedTextField = null;
        jLabel = null;
        val = "N/A";
        this.format = format;
        initialize(1);
    }

    public LabelAndFormattedTextField(javax.swing.JFormattedTextField.AbstractFormatter formatter) {
        jFormattedTextField = null;
        jLabel = null;
        val = "N/A";
        this.formatter = formatter;
        initialize(2);
    }

    public LabelAndFormattedTextField(javax.swing.JFormattedTextField.AbstractFormatterFactory factory) {
        jFormattedTextField = null;
        jLabel = null;
        val = "N/A";
        this.factory = factory;
        initialize(3);
    }

    public LabelAndFormattedTextField(javax.swing.JFormattedTextField.AbstractFormatterFactory factory, Object currentValue) {
        jFormattedTextField = null;
        jLabel = null;
        val = "N/A";
        this.factory = factory;
        this.currentValue = currentValue;
        initialize(4);
    }

    public LabelAndFormattedTextField(Object currentValue) {
        jFormattedTextField = null;
        jLabel = null;
        val = "N/A";
        this.currentValue = currentValue;
        initialize(5);
    }

    private void initialize(int type) {
        FlowLayout flowLayout2 = new FlowLayout();
        setLayout(flowLayout2);
        setSize(306, 27);
        flowLayout2.setHgap(2);
        flowLayout2.setVgap(2);
        add(getJLabel(), null);
        add(getJFormattedTextField(type), null);
    }

    private JLabel getJLabel() {
        if (jLabel == null) {
            jLabel = new JLabel();
            jLabel.setSize(150, 23);
            jLabel.setText("N/A");
            jLabel.setPreferredSize(new Dimension(150, 23));
        }
        return jLabel;
    }

    private JFormattedTextField getJFormattedTextField(int type) {
        if (jFormattedTextField == null) {
            switch (type) {
                case 0: // '\0'
                    jFormattedTextField = new JFormattedTextField();
                    break;

                case 1: // '\001'
                    jFormattedTextField = new JFormattedTextField(format);
                    break;

                case 2: // '\002'
                    jFormattedTextField = new JFormattedTextField(formatter);
                    break;

                case 3: // '\003'
                    jFormattedTextField = new JFormattedTextField(factory);
                    break;

                case 4: // '\004'
                    jFormattedTextField = new JFormattedTextField(factory, currentValue);
                    break;

                case 5: // '\005'
                    jFormattedTextField = new JFormattedTextField(currentValue);
                    break;

                default:
                    jFormattedTextField = new JFormattedTextField();
                    break;
            }
            jFormattedTextField.setSize(150, 23);
            jFormattedTextField.setText(val);
            jFormattedTextField.setPreferredSize(new Dimension(150, 23));
            jFormattedTextField.addKeyListener(new KeyAdapter() {
                public void keyReleased(KeyEvent e) {
                    setVal(jFormattedTextField.getText());
                }
            });
        }
        return jFormattedTextField;
    }

    private JFormattedTextField getFormattedTextField() {
        return jFormattedTextField;
    }

    public void setLabel(String txt) {
        getJLabel().setText(txt);
    }

    public String getLabel() {
        return getJLabel().getText();
    }

    public String getVal() {
        return val;
    }

    public void setVal(String val) {
        String old = val;
        this.val = val;
        firePropertyChange("val", old, val);
        jFormattedTextField.setText(val);
    }
    private JFormattedTextField jFormattedTextField;
    private JLabel jLabel;
    private String val;
    private Format format;
    private javax.swing.JFormattedTextField.AbstractFormatter formatter;
    private javax.swing.JFormattedTextField.AbstractFormatterFactory factory;
    private Object currentValue;
}
